package org.dragonnova.business.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LocalInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);

		if (modelAndView != null) {
			String realViewName = modelAndView.getViewName();
			if (modelAndView.getModelMap().get("_master") != null) {
				System.out.println(modelAndView.getModelMap().get("_master"));
				boolean _master = Boolean.valueOf(modelAndView.getModelMap()
						.get("_master").toString());
				if (_master) {
					modelAndView.setViewName("/common/master");
					modelAndView.addObject("contentName", realViewName);
				}
			}
			modelAndView.addObject("ctx", request.getContextPath());
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

}
